/*
 * @Author: 肖芬芳 1911683843@qq.com
 * @Date: 2022-09-23 16:18:32
 * @LastEditors: 肖芬芳 1911683843@qq.com
 * @LastEditTime: 2022-12-02 09:49:45
 * @FilePath: \node_study\routes\admin\system\role\list.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const {
    Role
} = require('../../../../model/role.js');

module.exports = async (req, res) => {
    // 获取post参数
    // console.log(req.fields);
    let rolename = req.fields.role;
    let pageSize = req.fields.pageSize;
    let currentPage = req.fields.currentPage;
    // console.log(rolename)
    if (rolename == '') {
        // 查询所有用户，逆序展示
        let roles = await Role.find().sort('-createTime').skip((currentPage - 1) * pageSize).limit(pageSize);
        // console.log(roles)
        let totalCount = await Role.find().count();
        res.send({
            'code': 0,
            "totalCount": totalCount,
            "data": roles
        });
    } else {
        //模糊查询，生成正则表达式new RegExp(req.fields.role)
        let roles = await Role.find({
            'role': new RegExp(req.fields.role)
        }).sort('-createTime').skip((currentPage - 1) * pageSize).limit(pageSize);
        let totalCount = roles.length;
        res.send({
            'code': 0,
            "totalCount": totalCount,
            "data": roles
        });
    }
}